package com.lelv.business.route.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.lelv.business.route.bo.IRouteBo;
import com.lelv.business.route.model.Route;
import com.lelv.business.route.model.RoutePre;
import com.lelv.framework.action.BaseAction;
import com.lelv.framework.bo.IUserBo;
import com.lelv.framework.model.User;
import com.lelv.framework.plugin.LelvUtils;
import com.lelv.framework.plugin.Page;

public class RouteAction extends BaseAction{

	private IRouteBo routeBo;
	private IUserBo userBo;
	private Page page = new Page();
	private List<Route> routeList = new ArrayList<Route>();
	private List<RoutePre> preList = new ArrayList<RoutePre>();
	private Route route = new Route();
	private User user = new User();
	private File myFile;
	private String myFileContentType;
	private String myFileFileName;
	private Integer state = 0;

	public String getRoutesByUser(){

		this.user = userBo.getUserById(user.getUserId());
		this.preList = this.routeBo.getRoutesByUser(user);

		return SUCCESS;
	}

	public String getRoutes(){

		this.page = routeBo.getRoutes(page,getCurrentUserId(),(Integer)getSession().getAttribute("adminLevel"));
		routeList = page.getList();

		return "getRoutesOK";
	}

	public String getRoutesWeb(){

		this.page = routeBo.getRoutes(page);
		routeList = page.getList();

		return "getRoutesOK";
	}

	public String findRoutes(){

		this.routeList = this.routeBo.findRoutes(route);

		return "findOK";
	}

	public String addRoute() throws Exception{

		//基于myFile创建一个文件输入流
        InputStream is = new FileInputStream(myFile);
        // 设置上传文件目录
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/framework/advImgs");

        if(state != 0){
        	this.myFileFileName = LelvUtils.createSystemFileName(myFileFileName);
        }

        // 设置目标文件
        File toFile = new File(uploadPath, this.getMyFileFileName());

        // 创建一个输出流
        OutputStream os = new FileOutputStream(toFile);

        //设置缓存
        byte[] buffer = new byte[1024];

        int length = 0;

        //读取myFile文件输出到toFile文件中
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }

        //关闭输入流
        is.close();

        //关闭输出流
        os.close();

		route.setCreatorId(getCurrentUserId());
		route.setCreateTime(new Date());
		route.setRoutePic(myFileFileName);
		this.routeBo.addRoute(route);

		return "addOK";
	}

	public String deleteRoute(){

		this.routeBo.deleteRoute(route.getRouteId());

		return SUCCESS;
	}

	public String updateRouteForward(){

		this.route = this.routeBo.getRouteById(route.getRouteId());

		return SUCCESS;
	}

	public String updateRoute(){

		Route temp = this.routeBo.getRouteById(route.getRouteId());
		BeanUtils.copyProperties(route, temp, new String[]{"routeId","routeDays","routeStartDate","routeEndDate","routeDest","creatorId","createTime","routePres"});
		this.routeBo.updateRoute(temp);
		return SUCCESS;
	}

	public String getRouteDetial(){

		this.route = this.routeBo.getRouteById(route.getRouteId());

		return "getRouteOK";
	}

	public IRouteBo getRouteBo() {
		return routeBo;
	}

	public void setRouteBo(IRouteBo routeBo) {
		this.routeBo = routeBo;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public List<Route> getRouteList() {
		return routeList;
	}

	public void setRouteList(List<Route> routeList) {
		this.routeList = routeList;
	}


	public Route getRoute() {
		return route;
	}


	public void setRoute(Route route) {
		this.route = route;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public IUserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(IUserBo userBo) {
		this.userBo = userBo;
	}

	public List<RoutePre> getPreList() {
		return preList;
	}

	public void setPreList(List<RoutePre> preList) {
		this.preList = preList;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

}
